<template>
	<view>
		<!-- <button @click="login">登录</button> -->

		<view>
			<!-- 登录弹框 -->
			<u-popup :show="phoneModalShow" mode="bottom" border-radius="14">
				<view class="sq_box">
					<view class="info_title">邀请您补全个人信息</view>
					<view class="info_tips">如果已设置，请忽略</view>
					<view class="info_item">
						<view class="info_text">头像</view>
						<button class="info_btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" slot="right">
							<image class="info_img" :src="info.headUrl"></image>
						</button>
					</view>
					<view class="info_item">
						<view class="info_text">昵称</view>
						<input style="flex:1;border:1px solid #333" type="nickname" class="weui-input"
							@blur="userNameInput" v- placeholder="请输入昵称" />
					</view>
					<view
						style="text-align: center; display: flex; height: 50rpx;height: 75rpx;line-height: 75rpx; margin-top:0px; background: #e9614f;border-radius:40rpx; ">
						<view style="width: 50%;font-size: 30rpx;color: #fff;border-right:2rpx solid #4580c8;"
							@click="cancelInfo()">取消</view>
						<view style="width: 50%;color: #fff;font-size: 30rpx;">
							<view
								style="height: 75rpx;line-height: 75rpx;color: #fff;font-size: 30rpx;background: #4580c8;line-height: 75rpx;border-radius:0px 40rpx 40rpx 0px ;"
								@click="closeInfo()">
								确认
							</view>
						</view>
					</view>
				</view>
			</u-popup>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				phoneModalShow: false,
				info: {
					iv: '',
					encryptedData: '',
					headUrl: '',
					gender: null,
					userName: '',
					sessionKey: ''
				},
				avatar: "",
				nickname: '',
				token: '',
				id: '',
				_id: '',
				openid: ''
			};
		},
		onShow() {

			// if (uni.getStorageSync('token')) {
			// 	uni.switchTab({
			// 		url: '/pages/mine/index'
			// 	})
			// } else {
			// 	uni.showLoading({
			// 		title: '加载中'
			// 	});
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success(ress) {
			// 			console.log(ress.code)
			// 			uniCloud.callFunction({
			// 				name: 'login',
			// 				data: {
			// 					"code": ress.code
			// 				},
			// 				success: res => {
			// 					console.log(res, 'login')
			// 					that.id = res.result.uid
			// 					let token = res.result.token
			// 					that.openid = res.result.openid
			// 					that.token = token
			// 					uni.setStorageSync('openid', that.openid)
			// 					that.getuser(that.openid)
			// 					uni.setStorageSync('token', token)
			// 				}
			// 			})
			// 		}
			// 	})
			// }
		},
		onLoad() {
			if (uni.getStorageSync('token')) {
				uni.switchTab({
					url: '/pages/mine/index'
				})
			}else{
				this.get_login()
			} 
		},
		methods: {
			get_login(){
				var that = this
				uni.showLoading({
					title: '加载中'
				});
				uni.login({
					provider: 'weixin',
					success(ress) {
						console.log(ress.code)
						uniCloud.callFunction({
							name: 'login',
							data: {
								"code": ress.code
							},
							success: res => {
								console.log(res, 'login')
								that.id = res.result.uid
								let token = res.result.token
								that.openid = res.result.openid
								that.token = token
								uni.setStorageSync('openid', that.openid)
								that.getuser(that.openid)
								uni.setStorageSync('token', token)
							}
						})
					}
				})
			},
			getuser(openid) {
				var that = this
				const db = uniCloud.database();
				db.collection('user-list').where({
					_wxopenid: openid
				}).get().then(res => {

					if (res.result.data.length > 0) {
						uni.hideLoading();
						let data = res.result.data[0]
						//是否已存有头像和昵称
						that._id = data._id;
						if (data.active && data.username) {
							uni.setStorageSync('user', data)
							uni.showModal({
								title: '提示',
								content: '是否修改头像及昵称',
								success: function(res) {
									if (res.confirm) {
										uni.hideLoading();
										that.phoneModalShow = true
										that.type = 'set'
										console.log('用户点击确定');
									} else if (res.cancel) {
										console.log('用户点击取消');
										
										uni.switchTab({
											url: '/pages/mine/index'
										})
									}
								}
							});

						} else {
							that.type = 'set'
							uni.hideLoading();
							that.phoneModalShow = true
						}
					} else {
						// 没有此用户 添加
						that.type = 'add'
						uni.hideLoading();
						that.phoneModalShow = true
					}
				})
			},
			closeInfo() {
				if (this.type == 'add') {
					this.add_user()
				} else {
					if (!this.info.headUrl) {
						uni.showToast({
							icon: 'none',
							title: '请上传头像'
						});
						return;
					}
					uni.showLoading({
						title: '加载中'
					});
					const db = uniCloud.database();
					let obj = {
						username: this.info.userName,
						active: this.info.headUrl,
						wxtoken: this.token
					}
					db.collection('user-list').doc(this._id).update(obj).then(res => {
						uni.hideLoading();

						uni.setStorageSync('user', obj)
						uni.switchTab({
							url: '/pages/mine/index'
						})
					})
				}
			},
			add_user() {
				uni.showLoading({
					title: '加载中'
				});
				const db = uniCloud.database();
				db.collection('user-list').add({
					_wxopenid: this.openid,
					username: this.info.userName,
					active: this.info.headUrl,
					wxtoken: this.token,
					jifen_num: 0
				}).then(re => {
					uni.hideLoading();
					let obj = {
						_wxopenid: this.openid,
						username: this.info.userName,
						active: this.info.headUrl,
						wxtoken: this.token
					}
					uni.setStorageSync('user', obj)
					uni.switchTab({
						url: '/pages/mine/index'
					})
				})
			},
			cancelInfo() {
				this.phoneModalShow = false
				this.get_login()
			},
			uploadImg(filePath) {
				var that = this
				uniCloud.uploadFile({
					filePath: filePath, //要上传的文件对象
					cloudPath: Date.now() + '.jpg', //保存在云端的文件名，这里以时间戳命名
					success(res) {
						console.log(res, '33333')
						let imageUrl = res.fileID //云端返回的图片地址

						uniCloud.callFunction({
							name: 'getImage',
							data: {
								fileList: [imageUrl]
							}
						}).then(fil => {
							console.log(fil, 'fil')
							let imageUrl = fil.result.fileList[0].download_url
							that.info.headUrl = imageUrl
							uni.hideLoading()
						})
					},
					fail(err) {
						console.log(err)
					},
					complete() {
						uni.hideLoading()
					}
				});

			},
			onChooseAvatar(e) {

				console.log(e, 'eee')
				// this.head_image()
				this.info.headUrl = e.detail.avatarUrl
				this.uploadImg(e.detail.avatarUrl); //tmp的图片临时路径
			},
			userNameInput(e) {
				this.info.userName = e.detail.value
			},
			login() {
				this.phoneModalShow = true;
			}

		},
	}
</script>
<style scoped>
	.sq_box {
		width: 700rpx;
		/* min-height: 500rpx; */
		border-radius: 10px;
		overflow: hidden;
		padding: 10px;
		box-sizing: border-box;
		background: #fff;
		margin: 0 auto;
	}

	.info_view {
		background: #E2E4E9;
		border-radius: 20rpx 20rpx 0 0;
		padding: 40rpx;
	}

	.info_title {
		text-align: center;
	}

	.info_tips {
		color: gray;
		font-size: 24rpx;
		text-align: center;
	}

	.info_item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
		margin-bottom: 30rpx;
	}

	.info_item input {
		padding: 10px;
		box-sizing: border-box;
		height: 100%;
		margin-left: 20px;
		border-radius: 10px;
	}

	.info_btn {
		border-radius: 100%;
		margin: 0 auto;
		padding: 0;
		width: 70rpx;
		height: 70rpx;
	}

	.weui-input {
		text-align: center;
	}
</style>